using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;
namespace DataAccess
{
	/// <summary>
	/// Strongly-typed collection for the TblCustomer class.
	/// </summary>
    [Serializable]
	public partial class TblCustomerCollection : ActiveList<TblCustomer, TblCustomerCollection>
	{	   
		public TblCustomerCollection() {}
        
        /// <summary>
		/// Filters an existing collection based on the set criteria. This is an in-memory filter
		/// Thanks to developingchris for this!
        /// </summary>
        /// <returns>TblCustomerCollection</returns>
		public TblCustomerCollection Filter()
        {
            for (int i = this.Count - 1; i > -1; i--)
            {
                TblCustomer o = this[i];
                foreach (SubSonic.Where w in this.wheres)
                {
                    bool remove = false;
                    System.Reflection.PropertyInfo pi = o.GetType().GetProperty(w.ColumnName);
                    if (pi.CanRead)
                    {
                        object val = pi.GetValue(o, null);
                        switch (w.Comparison)
                        {
                            case SubSonic.Comparison.Equals:
                                if (!val.Equals(w.ParameterValue))
                                {
                                    remove = true;
                                }
                                break;
                        }
                    }
                    if (remove)
                    {
                        this.Remove(o);
                        break;
                    }
                }
            }
            return this;
        }
		
		
	}
	/// <summary>
	/// This is an ActiveRecord class which wraps the TblCustomers table.
	/// </summary>
	[Serializable]
	public partial class TblCustomer : ActiveRecord<TblCustomer>, IActiveRecord
	{
		#region .ctors and Default Settings
		
		public TblCustomer()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}
		
		private void InitSetDefaults() { SetDefaults(); }
		
		public TblCustomer(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}
        
		public TblCustomer(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}
		 
		public TblCustomer(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}
		
		protected static void SetSQLProps() { GetTableSchema(); }
		
		#endregion
		
		#region Schema and Query Accessor	
		public static Query CreateQuery() { return new Query(Schema); }
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}
		}
		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("TblCustomers", TableType.Table, DataService.GetInstance("DataAccess"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "ID";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = true;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarName = new TableSchema.TableColumn(schema);
				colvarName.ColumnName = "Name";
				colvarName.DataType = DbType.String;
				colvarName.MaxLength = -1;
				colvarName.AutoIncrement = false;
				colvarName.IsNullable = true;
				colvarName.IsPrimaryKey = false;
				colvarName.IsForeignKey = false;
				colvarName.IsReadOnly = false;
				colvarName.DefaultSetting = @"";
				colvarName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarName);
				
				TableSchema.TableColumn colvarFamily = new TableSchema.TableColumn(schema);
				colvarFamily.ColumnName = "Family";
				colvarFamily.DataType = DbType.String;
				colvarFamily.MaxLength = -1;
				colvarFamily.AutoIncrement = false;
				colvarFamily.IsNullable = true;
				colvarFamily.IsPrimaryKey = false;
				colvarFamily.IsForeignKey = false;
				colvarFamily.IsReadOnly = false;
				colvarFamily.DefaultSetting = @"";
				colvarFamily.ForeignKeyTableName = "";
				schema.Columns.Add(colvarFamily);
				
				TableSchema.TableColumn colvarAddress = new TableSchema.TableColumn(schema);
				colvarAddress.ColumnName = "Address";
				colvarAddress.DataType = DbType.String;
				colvarAddress.MaxLength = -1;
				colvarAddress.AutoIncrement = false;
				colvarAddress.IsNullable = true;
				colvarAddress.IsPrimaryKey = false;
				colvarAddress.IsForeignKey = false;
				colvarAddress.IsReadOnly = false;
				colvarAddress.DefaultSetting = @"";
				colvarAddress.ForeignKeyTableName = "";
				schema.Columns.Add(colvarAddress);
				
				TableSchema.TableColumn colvarArea = new TableSchema.TableColumn(schema);
				colvarArea.ColumnName = "Area";
				colvarArea.DataType = DbType.String;
				colvarArea.MaxLength = -1;
				colvarArea.AutoIncrement = false;
				colvarArea.IsNullable = true;
				colvarArea.IsPrimaryKey = false;
				colvarArea.IsForeignKey = false;
				colvarArea.IsReadOnly = false;
				colvarArea.DefaultSetting = @"";
				colvarArea.ForeignKeyTableName = "";
				schema.Columns.Add(colvarArea);
				
				TableSchema.TableColumn colvarMobile = new TableSchema.TableColumn(schema);
				colvarMobile.ColumnName = "Mobile";
				colvarMobile.DataType = DbType.String;
				colvarMobile.MaxLength = -1;
				colvarMobile.AutoIncrement = false;
				colvarMobile.IsNullable = true;
				colvarMobile.IsPrimaryKey = false;
				colvarMobile.IsForeignKey = false;
				colvarMobile.IsReadOnly = false;
				colvarMobile.DefaultSetting = @"";
				colvarMobile.ForeignKeyTableName = "";
				schema.Columns.Add(colvarMobile);
				
				TableSchema.TableColumn colvarTelephone = new TableSchema.TableColumn(schema);
				colvarTelephone.ColumnName = "Telephone";
				colvarTelephone.DataType = DbType.String;
				colvarTelephone.MaxLength = -1;
				colvarTelephone.AutoIncrement = false;
				colvarTelephone.IsNullable = true;
				colvarTelephone.IsPrimaryKey = false;
				colvarTelephone.IsForeignKey = false;
				colvarTelephone.IsReadOnly = false;
				colvarTelephone.DefaultSetting = @"";
				colvarTelephone.ForeignKeyTableName = "";
				schema.Columns.Add(colvarTelephone);
				
				TableSchema.TableColumn colvarProvinceId = new TableSchema.TableColumn(schema);
				colvarProvinceId.ColumnName = "ProvinceId";
				colvarProvinceId.DataType = DbType.Int32;
				colvarProvinceId.MaxLength = 0;
				colvarProvinceId.AutoIncrement = false;
				colvarProvinceId.IsNullable = true;
				colvarProvinceId.IsPrimaryKey = false;
				colvarProvinceId.IsForeignKey = true;
				colvarProvinceId.IsReadOnly = false;
				colvarProvinceId.DefaultSetting = @"";
				
					colvarProvinceId.ForeignKeyTableName = "TblProvince";
				schema.Columns.Add(colvarProvinceId);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["DataAccess"].AddSchema("TblCustomers",schema);
			}
		}
		#endregion
		
		#region Props
		  
		[XmlAttribute("Id")]
		[Bindable(true)]
		public int Id 
		{
			get { return GetColumnValue<int>(Columns.Id); }
			set { SetColumnValue(Columns.Id, value); }
		}
		  
		[XmlAttribute("Name")]
		[Bindable(true)]
		public string Name 
		{
			get { return GetColumnValue<string>(Columns.Name); }
			set { SetColumnValue(Columns.Name, value); }
		}
		  
		[XmlAttribute("Family")]
		[Bindable(true)]
		public string Family 
		{
			get { return GetColumnValue<string>(Columns.Family); }
			set { SetColumnValue(Columns.Family, value); }
		}
		  
		[XmlAttribute("Address")]
		[Bindable(true)]
		public string Address 
		{
			get { return GetColumnValue<string>(Columns.Address); }
			set { SetColumnValue(Columns.Address, value); }
		}
		  
		[XmlAttribute("Area")]
		[Bindable(true)]
		public string Area 
		{
			get { return GetColumnValue<string>(Columns.Area); }
			set { SetColumnValue(Columns.Area, value); }
		}
		  
		[XmlAttribute("Mobile")]
		[Bindable(true)]
		public string Mobile 
		{
			get { return GetColumnValue<string>(Columns.Mobile); }
			set { SetColumnValue(Columns.Mobile, value); }
		}
		  
		[XmlAttribute("Telephone")]
		[Bindable(true)]
		public string Telephone 
		{
			get { return GetColumnValue<string>(Columns.Telephone); }
			set { SetColumnValue(Columns.Telephone, value); }
		}
		  
		[XmlAttribute("ProvinceId")]
		[Bindable(true)]
		public int? ProvinceId 
		{
			get { return GetColumnValue<int?>(Columns.ProvinceId); }
			set { SetColumnValue(Columns.ProvinceId, value); }
		}
		
		#endregion
		
		
		#region PrimaryKey Methods		
		
        protected override void SetPrimaryKey(object oValue)
        {
            base.SetPrimaryKey(oValue);
            
            SetPKValues();
        }
        
		
		public DataAccess.TblCusFolderCollection TblCusFolderRecords()
		{
			return new DataAccess.TblCusFolderCollection().Where(TblCusFolder.Columns.CusId, Id).Load();
		}
		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a TblProvince ActiveRecord object related to this TblCustomer
		/// 
		/// </summary>
		public DataAccess.TblProvince TblProvince
		{
			get { return DataAccess.TblProvince.FetchByID(this.ProvinceId); }
			set { SetColumnValue("ProvinceId", value.Id); }
		}
		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
        
        
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varName,string varFamily,string varAddress,string varArea,string varMobile,string varTelephone,int? varProvinceId)
		{
			TblCustomer item = new TblCustomer();
			
			item.Name = varName;
			
			item.Family = varFamily;
			
			item.Address = varAddress;
			
			item.Area = varArea;
			
			item.Mobile = varMobile;
			
			item.Telephone = varTelephone;
			
			item.ProvinceId = varProvinceId;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,string varName,string varFamily,string varAddress,string varArea,string varMobile,string varTelephone,int? varProvinceId)
		{
			TblCustomer item = new TblCustomer();
			
				item.Id = varId;
			
				item.Name = varName;
			
				item.Family = varFamily;
			
				item.Address = varAddress;
			
				item.Area = varArea;
			
				item.Mobile = varMobile;
			
				item.Telephone = varTelephone;
			
				item.ProvinceId = varProvinceId;
			
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}
		#endregion
        
        
        
        #region Typed Columns
        
        
        public static TableSchema.TableColumn IdColumn
        {
            get { return Schema.Columns[0]; }
        }
        
        
        
        public static TableSchema.TableColumn NameColumn
        {
            get { return Schema.Columns[1]; }
        }
        
        
        
        public static TableSchema.TableColumn FamilyColumn
        {
            get { return Schema.Columns[2]; }
        }
        
        
        
        public static TableSchema.TableColumn AddressColumn
        {
            get { return Schema.Columns[3]; }
        }
        
        
        
        public static TableSchema.TableColumn AreaColumn
        {
            get { return Schema.Columns[4]; }
        }
        
        
        
        public static TableSchema.TableColumn MobileColumn
        {
            get { return Schema.Columns[5]; }
        }
        
        
        
        public static TableSchema.TableColumn TelephoneColumn
        {
            get { return Schema.Columns[6]; }
        }
        
        
        
        public static TableSchema.TableColumn ProvinceIdColumn
        {
            get { return Schema.Columns[7]; }
        }
        
        
        
        #endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"ID";
			 public static string Name = @"Name";
			 public static string Family = @"Family";
			 public static string Address = @"Address";
			 public static string Area = @"Area";
			 public static string Mobile = @"Mobile";
			 public static string Telephone = @"Telephone";
			 public static string ProvinceId = @"ProvinceId";
						
		}
		#endregion
		
		#region Update PK Collections
		
        public void SetPKValues()
        {
}
        #endregion
    
        #region Deep Save
		
        public void DeepSave()
        {
            Save();
            
}
        #endregion
	}
}
